package com.xiaoxin.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.xiaoxin.admin.service.AdminService;
import com.xiaoxin.commons.utils.Constants;
import com.xiaoxin.model.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021/9/14 18:09
 **/

@RestController
@RequestMapping("/system/users")
public class SystemController {


    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /*
     * 获取验证码图片
     * */
    @GetMapping("/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        // 生成验证码对象
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(300, 100);
        String code = captcha.getCode();
        // 验证码存入redis
        redisTemplate.opsForValue().set(Constants.CAP_CODE + uuid, code);
        // 输出验证码图片
        captcha.write(response.getOutputStream());

    }

    /*
     * 用户登录
     * */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        Map token = adminService.login(map);
        return ResponseEntity.ok(token);
    }

    /*
    * 获取用户信息
    * */
    @PostMapping("/profile")
    public ResponseEntity profile(){
        AdminVo vo=adminService.profile();
        return ResponseEntity.ok(vo);
    }
}
